package hlhj.fhp.supreme.customview

import android.content.Context
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.PopupWindow
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.adapters.CityAdp
import hlhj.fhp.supreme.javabean.CityBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import kotlinx.android.synthetic.main.city_pop.view.*

/**
 * Created by Administrator on 2018\4\3 0003.
 */
class CityPop(private val context: Context,private val listener:OnResult) :PopupWindow() {
    private lateinit var cityAdp:CityAdp
    private lateinit var pAdp:CityAdp
    private lateinit var aAdp:CityAdp

    private val cDatas = ArrayList<CityBean.DataBean>()
    private val pDatas = ArrayList<CityBean.DataBean>()
    private val aDatas = ArrayList<CityBean.DataBean>()

    private var ctv=""
    private var ptv=""
    private var atv=""
    private var cid=""
    private var pid=""
    private var aid=""

    init {
        this.width=LinearLayout.LayoutParams.MATCH_PARENT
        this.height=context.resources.displayMetrics.heightPixels/2
        setPop()
    }

    private fun setPop(){
        animationStyle=R.style.class_pop_style
        contentView=LayoutInflater.from(context).inflate(R.layout.city_pop,null)

        initList()

        OkGo.post<CityBean>(Urls.BaseUrl+Urls.CITY)
                .params("parent_id",0)
                .execute(object : JsonCallBack<CityBean>(){

                    override fun onError(response: Response<CityBean>?) {

                    }
                    override fun onSuccess(response: Response<CityBean>?) {
                        val cityBean = response?.body()
                        if (cityBean?.code==1){
                            cDatas.clear()
                            cDatas.addAll(cityBean.data)
                            cityAdp.notifyDataSetChanged()
                        }
                    }
                })
    }

    private fun initList() {
        cityAdp=CityAdp(cDatas,context)
        pAdp= CityAdp(pDatas,context)
        aAdp= CityAdp(aDatas,context)
        contentView.cityList.adapter=cityAdp
        contentView.provenceList.adapter=pAdp
        contentView.araList.adapter=aAdp


        contentView.cityList.setOnItemClickListener { parent, view, position, id ->
            contentView.cTv.text=cDatas[position].name
            ctv=cDatas[position].name
            cid=cDatas[position].id
            OkGo.post<CityBean>(Urls.BaseUrl+Urls.CITY)
                    .params("parent_id",cDatas[position].id)
                    .execute(object :JsonCallBack<CityBean>(){

                        override fun onSuccess(response: Response<CityBean>?) {
                            val bean = response?.body()
                            if (bean?.code==1){
                                pDatas.clear()
                                pDatas.addAll(bean.data)
                                pAdp.notifyDataSetChanged()
                            }

                        }
                    })


        }

        contentView.provenceList.setOnItemClickListener { parent, view, position, id ->
            contentView.pTv.text=pDatas[position].name
            ptv=pDatas[position].name
            pid=pDatas[position].id

            OkGo.post<CityBean>(Urls.BaseUrl+Urls.CITY)
                    .params("parent_id",pDatas[position].id)
                    .execute(object :JsonCallBack<CityBean>(){

                        override fun onSuccess(response: Response<CityBean>?) {
                            val bean = response?.body()
                            if (bean?.code==1){
                                aDatas.clear()
                                aDatas.addAll(bean.data)
                                aAdp.notifyDataSetChanged()
                            }

                        }
                    })

        }
        contentView.araList.setOnItemClickListener { parent, view, position, id ->
            contentView.aTv.text=aDatas[position].name
            aid=aDatas[position].id
            atv=aDatas[position].name
            listener.onResult(ctv,ptv,atv,cid,pid,aid)
            dismiss()
        }

        contentView.btExit.setOnClickListener {
            dismiss()
        }
    }

    interface OnResult{
        fun onResult(cTv:String,pTv:String,aTv:String,cid:String,pid:String,aId:String)
    }

}